//
//  IslanTableview.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/3/2.
//  Copyright © 2016年 game. All rights reserved.

#import "IslanTableview.h"
#import "IsLanTabCell.h"
#import "IslanListModel.h"
#import "UIImage+GIF.h"
#import "MJRefresh.h"
@interface IslanTableview ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) NSUserDefaults *myud;
@property (nonatomic,strong) IslanListModel *listModel;
@property (nonatomic,strong) FunPagerModel *pageModel;
@property (nonatomic,strong) NSMutableArray *listAry;
@property (nonatomic,assign) NSInteger pageCont;
@property (nonatomic,strong) NSMutableDictionary *AchiveDataDic;
@end

@implementation IslanTableview

-(NSMutableDictionary *)AchiveDataDic{
    if (!_AchiveDataDic) {
        _AchiveDataDic = [NSMutableDictionary dictionary];
    }
    return _AchiveDataDic;
}

-(NSMutableArray *)listAry{
    if (!_listAry) {
        _listAry = [NSMutableArray array];
    }
    return _listAry;
}

-(void)getDateWhithPage:(NSInteger)page AndDic:(NSDictionary *)dic{

    NSMutableDictionary *tabDic = [NSMutableDictionary dictionary];
    
    [tabDic addEntriesFromDictionary:@{
                                       @"latitude":[self.myud valueForKey:@"latitude"],
                                       @"longitude":[self.myud valueForKey:@"longitude"],
                                       @"page":[NSString stringWithFormat:@"%ld",(long)page],
                                 
                                       }];
    
    [tabDic addEntriesFromDictionary:dic];

    
    [self.AchiveDataDic addEntriesFromDictionary:dic];
    
    [[Manager sharedManager]ResponseWhithURL:self.url AndParameter:tabDic AndCompletionType:^(id responseObject, DIEError *error) {
         
        if (!error) {
            
            
            if ([responseObject[@"status"] isEqualToString:@"SUCCESS"]) {
            
                self.listModel = [[IslanListModel alloc]initWithDictionary:responseObject[@"data"] error:nil];
                
                self.pageModel = self.listModel.pager;
                
                [self.listAry addObjectsFromArray:self.listModel.list];
                
                [[NSNotificationCenter defaultCenter]postNotificationName:@"MapShowAry" object:nil userInfo:@{@"sss":self.listAry}];//发送通知 到地图视图  发送坐标数组

                self.isShowImage = YES;
                
                [self reloadData];
                [self.header endRefreshing];
                [self.footer endRefreshing];
            
            
            }else{
            
                self.isShowImage = YES;
                
                [self reloadData];
                [self.header endRefreshing];
                [self.footer endRefreshing];
            
            }
            
            
        }else{
            self.isShowFile = YES;
            [self reloadData];
            [self.header endRefreshing];
            [self.footer endRefreshing];
            [Manager GetHudWhithText:@"加载失败..." AndFatherView:self];

        }
    
    }];
}
#pragma mark--点击了添加时间的选择条件
-(void)setOrder:(NSString *)order{
    _order = order;

    NSDictionary *dic = @{@"order":_order};
    if ([_order isEqualToString:@""]) {
        dic = nil;
        [self.AchiveDataDic removeObjectForKey:@"order"];
    }
    
    [self.AchiveDataDic addEntriesFromDictionary: dic];
    
    [self.header beginRefreshing];
}

-(void)setRange:(NSDictionary *)range{
    _range = range;
    
    [self.AchiveDataDic addEntriesFromDictionary:_range];
    [self.header beginRefreshing];
}

-(void)setArea_code:(NSDictionary *)area_code{
    _area_code = area_code;
    
    [self.AchiveDataDic addEntriesFromDictionary:_area_code];
    [self.header beginRefreshing];
}
//
//#pragma mark--点击了筛选上的选择条件
-(void)setShaiXuanDic:(NSDictionary *)shaiXuanDic{
    _shaiXuanDic = shaiXuanDic;

    [self.AchiveDataDic addEntriesFromDictionary: _shaiXuanDic];
    [self.header beginRefreshing];
}
//
-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    if (self = [super initWithFrame:frame style:style]) {
        self.delegate = self;
        self.dataSource = self;
        
        [self registerNib:[UINib nibWithNibName:@"IsLanTabCell" bundle:nil] forCellReuseIdentifier:@"cell"];

        self.separatorStyle = 0;
        
        self.myud = [NSUserDefaults standardUserDefaults];

        self.header = [MJDIYHeader headerWithRefreshingTarget:self refreshingAction:@selector(headRefresh)];
        
        self.footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footRefresh)];

        self.footer.automaticallyHidden = YES;
        self.pageCont = 1;
        
        //添加通知  用于获取城市已经跟换
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(callBack) name:@"CityChange" object:nil];
        
        
         self.footer.automaticallyHidden = YES;
        
        
        __weak typeof(self) weakSelf = self;
        self.TapBlok = ^{
            
            [weakSelf headRefresh];
        };

    }
    return self;
}

-(void)setUrl:(NSString *)url{
    _url = url;
    [self getDateWhithPage:1 AndDic:self.dic];

}
- (void)callBack{
    
    [self.AchiveDataDic addEntriesFromDictionary:@{@"city":[self.myud valueForKey:@"cityPinY"]}];
    [self headRefresh];
    [self.Maindelget CityChange];
}

-(void)headRefresh{
    
    [self.listAry removeAllObjects];
    [self getDateWhithPage:1 AndDic:self.AchiveDataDic];
    
    self.pageCont = 1;
}

-(void)footRefresh{
    
    self.pageCont += 1;
    if (self.pageCont>self.pageModel.total_page.integerValue) {
        
        [self.footer noticeNoMoreData];
    }else{
        [self getDateWhithPage:self.pageCont AndDic:self.AchiveDataDic];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.listAry.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    IsLanTabCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if (self.listAry.count>0) {
        
        cell.model = self.listAry[indexPath.row];
    }
    
            return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return SCREEN_HEIGHT*0.13;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    IsLanTabCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    if ([self.Maindelget respondsToSelector:@selector(IsLanTabClickWhithDetail:AndLatitude:AndLongitude:AndLocaltion:AndID:)]) {
        [self.Maindelget IsLanTabClickWhithDetail:cell.detail AndLatitude:cell.latitude AndLongitude:cell.longitude AndLocaltion:cell.location AndID:cell.Id];
    }
    
}

-(void)dealloc{
    
//    [[NSNotificationCenter debugDescription]removeObserver:self forKeyPath:@"CityChange"];
}

@end